package br.com.portal.controllers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.portal.beans.Representante;

@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sessao = request.getSession(true);
		String usuario = request.getParameter("usuario");
		String senha = request.getParameter("senha");
		Representante representante = Representante.getRepresentanteDAO(usuario, senha);
		if(representante == null)
		{
			sessao.setAttribute("msg", "erro");
			response.sendRedirect("login.jsp");
		}
		else
		{
			sessao.setMaxInactiveInterval(3600);
			sessao.removeAttribute("msg");
			sessao.setAttribute("rep", representante);
			response.sendRedirect(request.getContextPath() + "/app/index.jsp");
		}
		
	}

}
